/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;
import DAO.*;
import POJO.*;
import java.sql.SQLException;
import java.util.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ben
 */
public class form_NhanVien extends javax.swing.JInternalFrame {

    /**
     * Creates new form form_NhanVien
     */
    public form_NhanVien() throws SQLException {
        initComponents();
        
        Load();
    }

    DAO_NhanVien nv = new DAO_NhanVien();
    DefaultTableModel model;
    
    public void Load() throws SQLException{
        List<POJO_NhanVien> list = nv.LoadList();
        model = new DefaultTableModel(null, new Object[]{
            "Mã nhân viên", "Tên nhân viên", "CMND", "Ngày Sinh", "Giới tính", "Địa chỉ", "Điện thoại", "Ngày bắt đầu"
        });
        for(POJO_NhanVien nhanVien : list){
            model.addRow(new Object[]{
                nhanVien.GetMaNV(),
                nhanVien.GetTenNV(),
                nhanVien.GetCMND(),
                nhanVien.GetNgaySinh(),
                nhanVien.GetGioiTinh(),
                nhanVien.GetDiaChi(), 
                nhanVien.GetDienThoai(),
                nhanVien.GetNgayBD(),
            });
        };
        tbNhanVien.setModel(model);
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnXoa = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbNhanVien = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        btnThem = new javax.swing.JButton();
        btnSua1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        txtDienThoai = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtMaNV = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtCMND = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtNgaySinh = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtNgayBD = new javax.swing.JTextField();
        txtHoTen = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cbNu = new javax.swing.JCheckBox();
        cbNam = new javax.swing.JCheckBox();

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        tbNhanVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Mã nhân viên", "Họ tên", "CMND", "Ngày sinh", "Giới tính", "Địa chỉ", "Điện thoại", "Ngày bắt đầu"
            }
        ));
        tbNhanVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbNhanVienMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbNhanVien);

        jLabel1.setText("QUẢN LÝ NHÂN VIÊN");

        btnThem.setText("Thêm");
        btnThem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnThemMouseClicked(evt);
            }
        });

        btnSua1.setText("Thoát");

        jLabel4.setText("Địa chỉ:");

        jLabel9.setText("CMND:");

        jLabel3.setText("Mã NV:");

        jLabel5.setText("Giới tính:");

        jLabel6.setText("Điện thoại:");

        jLabel7.setText("Ngày bắt đầu:");

        txtNgaySinh.setText("YYYY-MM-DD");

        jLabel10.setText("Ngày Sinh:");

        txtNgayBD.setText("YYYY-MM-DD");

        jLabel8.setText("Họ tên");

        cbNu.setText("Nữ");
        cbNu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbNuActionPerformed(evt);
            }
        });

        cbNam.setText("Nam");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 713, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSua1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(txtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(43, 43, 43)
                                .addComponent(txtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtDiaChi, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNgayBD, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbNu, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbNam, javax.swing.GroupLayout.Alignment.LEADING))))
                .addGap(50, 50, 50))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(278, 278, 278))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel8)
                                    .addComponent(txtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbNu)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cbNam)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNgayBD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSua1)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnSua))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnThemMouseClicked
        // TODO add your handling code here:
        //
        // Kiểm tra có trường nào còn trống không
        //
        if(TestEmpty() == true){
                POJO_NhanVien nhanVien = new POJO_NhanVien();


                nhanVien.SetMaNV(txtMaNV.getText());
                //
                //Kiểm tra CMND có phải là số không
                //
                if(TestNumber(txtCMND.getText()) == true){
                    nhanVien.SetCMND(txtCMND.getText());
                }
                else{
                    JOptionPane.showMessageDialog(null, "Vui lòng nhập lại số CMND");
                    return;
                }
                //
                //Kiểm tra checkbox được chọn có đúng không
                //
                if(cbNam.isSelected() && cbNu.isSelected()){
                    JOptionPane.showMessageDialog(null, "Bạn đã chọn sai giới tính, xin vui lòng chọn lại!");
                }
                else{
                    if(cbNu.isSelected()){
                        nhanVien.SetGioiTinh("Nữ");
                    }
                    if(cbNam.isSelected()){
                        nhanVien.SetGioiTinh("Nam");
                    }
                }
                
                nhanVien.SetDiaChi(txtDiaChi.getText());
                //
                // Kiểm tra số điện thoại có đúng là số không
                //
                if(TestNumber(txtDienThoai.getText()) == true){
                    nhanVien.SetDienThoai(txtDienThoai.getText());
                }
                else{
                    JOptionPane.showMessageDialog(null, "Vui lòng nhập lại số điện thoại!");
                    return;
                }

                nhanVien.SetTenNV(txtHoTen.getText());
                nhanVien.SetNgaySinh(Date.valueOf(txtNgaySinh.getText()));
                nhanVien.SetNgayBD(Date.valueOf(txtNgayBD.getText()));

                nv.Add(nhanVien);
                JOptionPane.showMessageDialog(null, "Thêm thành công");

                try {
                    Load();
                } catch (SQLException ex) {
                    Logger.getLogger(form_NhanVien.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else{
                   JOptionPane.showMessageDialog(null, "Xin vui lòng nhập đầy đủ thông tin");
            }
        
    }//GEN-LAST:event_btnThemMouseClicked

    private void cbNuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbNuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbNuActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        int index = tbNhanVien.getSelectedRow();
        
        if(index > 0){
            POJO_NhanVien nhanVien = new POJO_NhanVien();
            nhanVien.SetMaNV(txtMaNV.getText());

            nv.Delete(nhanVien);

            try {
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(form_NhanVien.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng chọn dòng cần xóa");
        }        
    }//GEN-LAST:event_btnXoaActionPerformed

    public boolean TestEmpty(){
        if(txtCMND.getText().isEmpty() || txtDiaChi.getText().isEmpty() || txtDienThoai.getText().isEmpty() || txtHoTen.getText().isEmpty() || txtMaNV.getText().isEmpty() || txtNgayBD.getText().isEmpty() || txtNgaySinh.getText().isEmpty()){
            return false;
        }
        return true;
    }
    
    public boolean TestNumber(String str){
        try {
            Integer.parseInt(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    private void tbNhanVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbNhanVienMouseClicked
        // TODO add your handling code here:
        int index = tbNhanVien.getSelectedRow();
        
        txtMaNV.setText(String.valueOf(model.getValueAt(index, 0)));
        
        if("Nam".equals(model.getValueAt(index, 4))){
            cbNam.setSelected(true);
            cbNu.setSelected(false);
        }
        else{
            cbNu.setSelected(true);
            cbNam.setSelected(false);
        }
        
        txtCMND.setText(String.valueOf(model.getValueAt(index, 2)));
        txtHoTen.setText(String.valueOf(model.getValueAt(index, 1)));
        txtDienThoai.setText(String.valueOf(model.getValueAt(index, 6)));
        txtNgaySinh.setText(String.valueOf(model.getValueAt(index, 3)));
        txtDiaChi.setText(String.valueOf(model.getValueAt(index, 5)));
        txtNgayBD.setText(String.valueOf(model.getValueAt(index, 7)));
    }//GEN-LAST:event_tbNhanVienMouseClicked

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        //
        // Kiểm tra có trường nào còn trống không
        //
        if(TestEmpty() == true){
                POJO_NhanVien nhanVien = new POJO_NhanVien();


                nhanVien.SetMaNV(txtMaNV.getText());
                //
                //Kiểm tra CMND có phải là số không
                //
                if(TestNumber(txtCMND.getText()) == true){
                    nhanVien.SetCMND(txtCMND.getText());
                }
                else{
                    JOptionPane.showMessageDialog(null, "Vui lòng nhập lại số CMND");
                    return;
                }
                //
                //Kiểm tra checkbox được chọn có đúng không
                //
                if(cbNam.isSelected() && cbNu.isSelected()){
                    JOptionPane.showMessageDialog(null, "Bạn đã chọn sai giới tính, xin vui lòng chọn lại!");
                }
                else{
                    if(cbNu.isSelected()){
                        nhanVien.SetGioiTinh("Nữ");
                    }
                    if(cbNam.isSelected()){
                        nhanVien.SetGioiTinh("Nam");
                    }
                }
                
                nhanVien.SetDiaChi(txtDiaChi.getText());
                //
                // Kiểm tra số điện thoại có đúng là số không
                //
                if(TestNumber(txtDienThoai.getText()) == true){
                    nhanVien.SetDienThoai(txtDienThoai.getText());
                }
                else{
                    JOptionPane.showMessageDialog(null, "Vui lòng nhập lại số điện thoại!");
                    return;
                }

                nhanVien.SetTenNV(txtHoTen.getText());
                nhanVien.SetNgaySinh(Date.valueOf(txtNgaySinh.getText()));
                nhanVien.SetNgayBD(Date.valueOf(txtNgayBD.getText()));

                nv.Update(nhanVien);
                JOptionPane.showMessageDialog(null, "Sửa thành công");

                try {
                    Load();
                } catch (SQLException ex) {
                    Logger.getLogger(form_NhanVien.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else{
                   JOptionPane.showMessageDialog(null, "Xin vui lòng nhập đầy đủ thông tin");
            }
    }//GEN-LAST:event_btnSuaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnSua1;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JCheckBox cbNam;
    private javax.swing.JCheckBox cbNu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbNhanVien;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDienThoai;
    private javax.swing.JTextField txtHoTen;
    private javax.swing.JTextField txtMaNV;
    private javax.swing.JTextField txtNgayBD;
    private javax.swing.JTextField txtNgaySinh;
    // End of variables declaration//GEN-END:variables
}
